package com.project.chat.pojo;

import lombok.Data;
import lombok.ToString;
import java.util.ArrayList;
import java.util.List;

@Data
@ToString
public class Room {

    private int id; //房间号
    private int num; //人数
    private int now_num; //当前人数
    private List<Person> persons; //当前角色
    private SettingTool setting;  //房间规则

    //管理员需要创建好房间号，人数，规则
    public Room(int id, int num, SettingTool setting){
        this.id = id;
        this.num = num;
        this.now_num = 0;
        this.persons = new ArrayList<>(); //创建容器
        this.setting = setting;
    }

    //当前房间增加人数
    public boolean addPerson(Person person) {
        if(num > now_num) {
            //增加人数
            persons.add(person);
            ++now_num;
            return true;
        }
        return false;
    }

    //退出房间
    public void deletePerson(String username){

        for(int i = 0; i < persons.size(); i++){
            if(persons.get(i).getName().equals(username)){
                persons.remove(i);
                --now_num;
                return;
            }
        }
    }

    //清空房间
    public void clearRoom(){
        this.persons = null;
        this.setting = null;
        this.num = 0;
        this.now_num = 0;
    }
}
